package com.linsen.erp.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsen.erp.admin.service.IOperationLogService;
import com.linsen.erp.admin.entity.OperationLogEntity;
import com.linsen.erp.admin.mapper.OperationLogMapper;
import com.linsen.erp.admin.provider.qo.OperationLogQo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author zhaoyong
 * @since 2023-07-24
 */
@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLogEntity> implements IOperationLogService {

    /** 封装分页列表查询的wrapper对象 */
    private Wrapper<OperationLogEntity> getWrapper(OperationLogQo operationLogQo){
        String module = operationLogQo.getModule();
        String content = operationLogQo.getContent();
        String url = operationLogQo.getUrl();
        String requestParam = operationLogQo.getRequestParam();
        String responseParam = operationLogQo.getResponseParam();
        Long executeTimeMin = operationLogQo.getExecuteTimeMin();
        Long executeTimeMax = operationLogQo.getExecuteTimeMax();
        String ip = operationLogQo.getIp();
        String createByName = operationLogQo.getCreateByName();
        LocalDateTime startTime = operationLogQo.getStartTime();
        LocalDateTime endTime = operationLogQo.getEndTime();

        return new LambdaQueryWrapper<OperationLogEntity>()
                .like(ObjectUtil.isNotEmpty(module),OperationLogEntity::getModule,module)
                .like(ObjectUtil.isNotEmpty(content),OperationLogEntity::getContent,content)
                .like(ObjectUtil.isNotEmpty(url),OperationLogEntity::getUrl,url)
                .like(ObjectUtil.isNotEmpty(requestParam),OperationLogEntity::getRequestParam,requestParam)
                .like(ObjectUtil.isNotEmpty(responseParam),OperationLogEntity::getResponseParam,responseParam)
                .ge(ObjectUtil.isNotNull(executeTimeMin),OperationLogEntity::getExecuteTime,executeTimeMin)
                .le(ObjectUtil.isNotNull(executeTimeMax),OperationLogEntity::getExecuteTime,executeTimeMax)
                .like(ObjectUtil.isNotEmpty(ip),OperationLogEntity::getIp,ip)
                .like(ObjectUtil.isNotEmpty(createByName),OperationLogEntity::getCreateByName,createByName)
                .ge(ObjectUtil.isNotNull(startTime),OperationLogEntity::getCreateTime, startTime)
                .le(ObjectUtil.isNotNull(endTime),OperationLogEntity::getCreateTime, endTime)
                .orderByDesc(OperationLogEntity::getCreateTime);
    }

    @Override
    public Page<OperationLogEntity> page(OperationLogQo qo) {
        return this.page(new Page<>(qo.getCurrent(), qo.getSize()), getWrapper(qo));
    }
}
